package org.itboys.trade.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.itboys.commons.CommonConstants.DF;
import org.itboys.commons.CommonConstants.StaticParam;
import org.itboys.commons.utils.tools.ImagePathUtils;
import org.itboys.member.service.MemberService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.oss.OssService;
import org.itboys.trade.constant.CommentConstant;
import org.itboys.trade.dto.WXCommentListDto;
import org.itboys.trade.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;

@Service
public class CommentService extends BaseService<Comment, Long> {
	@Autowired
	MemberService memberService;
	@Autowired
	private OssService ossService;
	@Resource(name = "orderDS")
	private MongoDataSource ds;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return ds;
	}

	@Override
	protected Class<Comment> getEntityClass() {
		return Comment.class;
	}

	public Map<String, Object> imageUpload(MultipartFile imgFile) {
		Map<String, Object> map = new HashMap<>();
		String tempFile = null;
		String image = null;
		try {
			if (imgFile != null && !imgFile.isEmpty()) {
				tempFile = ossService.upload(imgFile.getInputStream(), CommentConstant.OSS_PATH);
				image = CommentConstant.OSS_URL_PREFIX + CommentConstant.OSS_PATH + "/" + tempFile;
				System.out.println("not null");
				map.put("code", 1);
				map.put("imageUrl", image);
			} else {
				map.put("code", -1);
			}

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", -1);
		}
		return map;
	}

	public Page<WXCommentListDto> getWXCommentListDtos(Long proId, Long shopId) {
		Map<String, Object> param = Maps.newHashMap();
		if (null != proId) {
			param.put("proIds", proId);
		}
		if (null != shopId) {
			param.put("shopId", shopId);
		}
		param.put("deleted", false);
		param.put("orderByKey", "-ct");
		param.put("pageNo", 1);
		param.put("pageSize", 5);
		Page<Comment> page = page(param);
		List<WXCommentListDto> temp = page.getData().stream().map(c -> {
			WXCommentListDto dto = new WXCommentListDto();
			// Member member=memberService.getById(c.getUserId());
			dto.setId(c.getId());
			dto.setScore(c.getScore());
			dto.setComment(c.getText());
			if (null!=c.getImages()) {
				if (c.getImages().size() > 0) {
					dto.setImages(c.getImages());
					dto.getImages().forEach(s -> {
						s = ImagePathUtils.add(StaticParam.imagePath, s);
					});
				}
			}
			dto.setLogo(ImagePathUtils.add(StaticParam.imagePath, c.getMemberLogo()));
			dto.setTime(DF.FORMAT_YYYY_MM_dd.format(c.getCt()));
			dto.setName(c.getNickName());
			return dto;
		}).collect(Collectors.toList());
		Page<WXCommentListDto> result = new Page<>();
		result.setData(temp);
		result.setTotal(page.getTotal());
		return result;
	}

	public Page<WXCommentListDto> getWebCommentListDtos(Map<String, Object> param) {
		param.put("deleted", false);
		param.put("orderByKey", "-ct");
		Page<Comment> page = page(param);
		List<WXCommentListDto> temp = page.getData().stream().map(c -> {
			WXCommentListDto dto = new WXCommentListDto();
			// Member member=memberService.getById(c.getUserId());
			dto.setId(c.getId());
			dto.setScore(c.getScore());
			dto.setComment(c.getText());
			if (null!=c.getImages()) {
				if (c.getImages().size() > 0) {
					dto.setImages(c.getImages());
					dto.getImages().forEach(s -> {
						s = ImagePathUtils.add(StaticParam.imagePath, s);
					});
				}
			}
			dto.setName(c.getNickName());
			dto.setLogo(ImagePathUtils.add(StaticParam.imagePath, c.getMemberLogo()));
			dto.setTime(DF.FORMAT_YYYY_MM_dd.format(c.getCt()));
			return dto;
		}).collect(Collectors.toList());
		Page<WXCommentListDto> result = new Page<>();
		result.setData(temp);
		result.setTotal(page.getTotal());
		return result;
	}

}
